Whole numbers and fractional numbers are two different things on the Game Boy Advance, and knowing which one you have decides what a division, a multiplication or a rounding gives you.
A program keeps its numbers in named boxes called variables. hp = 10; makes a box named hp and puts 10 in it. Later lines can read the box, do arithmetic with what is inside, and put a new number back: hp = hp - 3; leaves 7.
Nearly everything a game does is arithmetic on numbers like that. Where something is (x and y), how fast it moves (speed), how much health is left, how many frames until the next shot - each one is a number in a box.
GBML has two kinds of number, and the difference is not cosmetic.
| Kind | What it holds | How you write one | Typical use |
|---|---|---|---|
int (a whole number) | 0, 7, -3, 1000 - never a fraction | 7 | counting: lives, coins, frames, which frame of an animation |
real (a number with a fraction) | 2.5, -0.75, and also 4.0 | 2.5 - it needs the point | measuring: position, speed, angle, how far through a fade |
The decimal point is the whole rule. 4 is a whole number. 4.0 is a real that happens to equal four. GBML looks at how you wrote it.
A variable keeps one kind for its whole life, decided by the first value you put in it. The only change allowed is one-way: put a fractional value into a variable that held a whole number, and from then on it is a real. The built-in position and speed variables (x, y, speed) are real, since a character moving at one and a half pixels a frame needs the fraction. Variables, types and scope covers the rule in full.
A phone or a PC has circuitry built to work with fractional numbers directly. The Game Boy Advance does not. Its processor can only do arithmetic on whole numbers - it has no floating point hardware, which is the name for that circuitry. Doing fractions in software instead would be slow, and a game has a fixed slice of time - about one sixtieth of a second - to finish every frame.
So a real is a whole number in disguise. It counts in steps of 1/4096. The value 1.5 is stored as 6144 of those steps, and 1.0 as 4096. This is called fixed-point, because the point never moves - there are always exactly the same number of steps between one whole number and the next. Everything below follows from that.
int | real | |
|---|---|---|
| Smallest step | 1 | 1/4096 (about 0.000244) |
| Range | about -2.1 billion to 2.1 billion | about -524,288 to 524,288 |
| Goes past the range | comes out wrong | comes out wrong |
Two things to take from that table:
real cannot hold every fraction. It holds the multiples of 1/4096 and nothing between. Ask for 0.1 and you get the multiple just below it: 409/4096, which is about 0.0999. Ask for a third and you get 1365/4096, about 0.33325. That is more than accurate enough for a position on a 240 by 160 screen. It is not the same as exact.This is where beginners meet the difference first.
A whole number divided by a whole number gives a whole number. The fraction is thrown away, not rounded - the answer is cut toward zero.
var a = 7 / 2; // 3, not 3.5
var b = -7 / 2; // -3
var c = 1 / 3; // 0
var d = 37 / 100; // 0That is correct when you want it: halving a pixel count, working out which group of eight frames you are in. It is a trap when you meant a fraction. timer / 40, with both sides whole, is 0 for thirty-nine frames and 1 on the fortieth, so anything built on it jumps instead of easing.
Make either side a real and you get a fractional answer. One decimal point is enough:
var e = 7 / 2.0; // 3.5
var f = game_frame() / 40.0; // 0.025, 0.05, 0.075 ... as intendedSay so when you mean whole-number division. div is the same operation as / between two whole numbers, written so nobody has to guess. mod gives what is left over instead: 7 mod 2 is 1.
var group = game_frame() div 8; // which eight-frame group we are in
var odd = game_frame() mod 2; // 0 or 1The compiler warns when it can see a whole-number division being used as a fraction. It cannot see every case, so the habit to build is the decimal point. Expressions and statements has the full account.
Dividing a real is exact to one step, and no better. With x holding 1.0, x / 3 is 1365/4096 - the closest step below a third. Multiply that back by three and you get 4095/4096, not 1. So do not test a real for being exactly equal to a value after arithmetic on it. Compare with < or >, or check that the difference is small.
Dividing by a fractional real is coarser. Just as in a multiply, the divisor is first cut down to steps of 1/64. 1 / 0.1 comes out near 10.67, not 10, because the 0.1 has become 0.09375. Dividing by a whole number has no such cut.
Dividing a real by something very small, or by zero, is safe. Because the divisor is cut to steps of 1/64, a divisor smaller than 1/64 (such as 0.001) would be cut down to nothing, and the game cannot divide by nothing. GBML never lets that happen when a real is on either side of the /. A divisor of 1/64 or more, positive or negative, gives the answer described above, unchanged. A divisor between 0 and 1/64 gives the exact answer, so 10 / 0.001 is 10240. A divisor of exactly 0 (a 0.0 or a whole 0) gives the largest number a real can hold, 524287.99, or the most negative one when the number being divided is negative. Zero divided by zero gives the largest. Nothing stops the game and nothing is shown, so if 0 should mean something special in your game, check for it first.
A big real divides correctly. A real of 8,192 or more used to come out wrong when divided, because the first step of the division is to scale the number up, and that step ran past the range (map_y * 56.0 / 1024.0 with map_y near 600 gave -4 instead of 32.8). The multiplication was fine; the division after it was not. Now a division by a power of two you typed in (/ 2.0, / 1024.0) is exact, and a division by any other real (typed in or held in a variable) is worked in two parts when the number being divided is 8,192 or more, so none of them wrap.
Dividing a whole number by 0 is safe too. /, div and mod between whole numbers never stop the game on a 0 divisor. 7 / 0 and 0 / 0 give the largest whole number, 2147483647, and -7 / 0 gives the most negative one, -2147483648. mod by 0 gives back the number you started with: 7 mod 0 is 7. The same holds for /= and %=. Nothing is shown when it happens, so if 0 should mean something special in your game, check for it first. A divisor you typed in as a plain nonzero number, such as x / 2, can never be 0, so it costs nothing extra.
Division is slower. The processor has no divide instruction, so a division takes more time than an addition or a multiplication. Use it where the arithmetic calls for it; just know it is the costly one of the four.
Multiplying a real by a whole number is exact: x * 2 is exactly double.
Multiplying a real by another real has an edge. To keep the answer from overflowing, the machine first cuts each of the two numbers down to steps of 1/64 (0.015625), then multiplies. For a number like 0.1, which is only 6 of those coarser steps, the loss is large: 100 * 0.1 comes out as 9.375, not 10.
Two habits keep you clear of it:
x / 10. Dividing by a whole number is exact to one 1/4096 step.x * 2, not 2 * x. With the whole number on the left, the machine treats it as a real and cuts x to 1/64 as well.Products overflow sooner than you might expect. A real tops out near 524,288, so 800 * 800 (640,000) does not fit. This is why the distance between two far-apart points is not sqrt(dx * dx + dy * dy): squaring a few hundred pixels already overflows. dist_to does that sum in whole numbers first and does not.
real into a whole numberFour functions do it, and they differ only in how they treat the fraction. All four hand back an int.
If you only want to round a real and keep working with a real, use floor, ceil and round. They round exactly like int_floor, int_ceil and int_round, but hand back a real with nothing after the point.
The naming rule, for every function in this category: a function with no prefix hands back a real, and an int_ function hands back a whole number. So floor is the real and int_floor is the int; the same goes for ceil and round. roll_real is the one deliberate exception and keeps its _real suffix. A spelling such as floor_real is not a name here, and the checker points you at floor. Dropping the prefix from int_part would leave a bare part, which says nothing, so the round-toward-zero rule has only its int_part form so far.
| Value | int_part | int_floor | int_ceil | int_round |
|---|---|---|---|---|
1.9 | 1 | 1 | 2 | 2 |
-1.9 | -1 | -2 | -1 | -2 |
2.5 | 2 | 2 | 3 | 3 |
-2.5 | -2 | -3 | -2 | -3 |
int_part drops the fraction, moving toward zero.int_floor always goes down, toward the more negative number.int_ceil always goes up.int_round goes to the nearest, and a tie goes away from zero.For "which grid cell or tile is this position in", use int_floor. A room's positions start at 0 in its top-left corner, but a position just past the left or top edge, or an offset such as x - 20, is negative, and there int_part lands one cell too far toward zero.
Remember that the division rule comes first. int_ceil(37 / 8) is 4, because 37 / 8 has already lost its fraction before int_ceil sees it. Write int_ceil(37 / 8.0) to get 5.
clamp, min, max, sign, abs, wrap, approach, square and power hand back a real, even when every number you gave them was whole. So picture = min(picture + 1, 5); puts a real into a whole-number variable and turns it into a real for good - the one-way change described above, which goes by the kind of the value and not by whether it has a fraction in it. The next thing that wants a whole number then refuses the variable, often in another file, which makes the cause hard to find.
Each has a twin that works on whole numbers only and hands back a whole number: clamp_int, min_int, max_int, sign_int, abs_int, wrap_int, approach_int, square_int and power_int. Use the twin for things you count (lives, slots, picture numbers) and the plain one for things you measure (position, speed). The _int at the END of a name means "whole numbers in, a whole number out". The int_ at the START of a name (int_floor and the others above) means "turns a real into a whole number".
The other functions that hand back a real (lerp, sqrt, the angle and direction ones, wave) have no twin, because what they give is a measurement with a fraction in it. If you do want the whole part of one of those, use int_floor, int_round or int_part on the answer.
Most functions in this category take real numbers and give one back. A whole number goes in without trouble, since it is a real with no fraction. The exceptions: the four int_ rounding functions give back an int, the nine _int twins (clamp_int, min_int, max_int, sign_int, abs_int, wrap_int, approach_int, square_int, power_int) take whole numbers and give one back, roll_int and roll_reseed work in whole numbers, roll_chance gives back a yes-or-no answer, power takes a whole number for its exponent, and roll_one_of gives back whatever kind of thing the list it picks from holds.
| For | Functions |
|---|---|
| Rounding to a whole number | int_part, int_floor, int_ceil, int_round - these four return an int. floor, ceil and round round the same way and return a real |
| The part after the point | frac_part keeps what int_part drops, with the same sign, so the two add back up to the number you started with |
| Size and sign | abs drops a minus sign, sign gives -1, 0 or 1, min and max pick the smaller or larger of two. For whole numbers there are abs_int, sign_int, min_int and max_int |
| The middle | average of two numbers - it cannot overflow the way (a + b) / 2 can |
| Powers | square is a number times itself, power is a number times itself a whole number of times. Answers over about 524,000 go wrong (square of anything past about 724 gets there). For whole numbers there are square_int and power_int, which go up to about 2.1 billion |
| Keeping a value in bounds | clamp stops at both ends (clamp_int does the same for whole numbers), wrap steps off one end and reappears at the other (wrap_int for whole numbers) |
| Going round and round | modulo is the remainder after dividing and is never negative (modulo_int for whole numbers). every is true once every so many frames |
| How far along | inverse_lerp turns a value into a 0-to-1 position between two numbers, ease bends a 0-to-1 position into a curve. noise is smooth randomness |
| Moving toward a target | approach steps toward a value without overshooting (approach_int for whole numbers), lerp blends between two values |
| Pulsing | wave rises and falls between two values on a set number of frames |
| Angles | angle_sin, angle_cos, angle_tan, angle_diff, angle_approach |
| Direction and distance | dir_to, dist_to, dir_x, dir_y. dist_sq is the distance squared, with no square root, for quick "which is closer" questions |
| Roots | sqrt - precise to 1/64, and always rounded down |
| Random numbers | roll_int, roll_real, roll_chance, roll_one_of picks one item out of a list, roll_reseed |
Angles are in degrees. 0 points right (east) and an increasing angle turns counter-clockwise on screen, because the screen's y axis counts down. Where the machine shows through has a diagram.
Random numbers are not random in the way a coin is. The machine has no clock to start them from, so every run of the same game rolls the same numbers in the same order until something calls roll_reseed. A common fix is to reseed from game_frame the first time the player presses a button, so the sequence depends on when a human pressed it.